package net.babelphish.altitudesig;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainPageScraper extends DataScraper {

	public MainPageScraper(String dataLocation) {
		super(dataLocation);
	}

	boolean processScrapedData(StringBuilder scrapedContent, GAME_TYPE gameType)
	{
		try {
			_playerName = findName(scrapedContent);
			_winLoss = findWinLoss(scrapedContent, gameType);
			_rating = findRating(scrapedContent, gameType);
			_ratings = findRatings(scrapedContent, gameType);
			_rank = findRank(scrapedContent, gameType);
		}
		catch(Exception e)
		{
			return false;
		}
		
		return true;
	}
	
	private Integer findRank(StringBuilder searchText, GAME_TYPE gameType) throws Exception {
		Pattern namePattern = Pattern.compile("<th>Rank.*?</th>.*?<td>([0-9]*).*?</td>.*?<td>([0-9]*).*?</td>",Pattern.DOTALL);
		Matcher matcher = namePattern.matcher(searchText);
		boolean matchFound = matcher.find();

		if (matchFound) {
			if (gameType == GAME_TYPE.BALL) {
				return Integer.parseInt(matcher.group(2).trim());
			}
			else if (gameType == GAME_TYPE.TBD)
			{
				return Integer.parseInt(matcher.group(1).trim());
			}
		}
		throw new Exception("Couldn't find Rank");
	}

	
	private String findName(StringBuilder searchText) throws Exception {
		Pattern namePattern = Pattern.compile("<title>(.*?) &ndash;");
		Matcher matcher = namePattern.matcher(searchText);
		boolean matchFound = matcher.find();

		if (matchFound) {
			return matcher.group(1).trim();
		} else {
			throw new Exception("Couldn't find Name");
		}
	}

	private Integer findRating(StringBuilder searchText, GAME_TYPE gameType) throws Exception {
		Pattern namePattern = Pattern.compile("id=\"tbd_rating_chart\"></span>.*?<span class=\"current_rating\">(.*?)</span>.*?<span class=\"current_rating\">(.*?)</span>",Pattern.DOTALL);
		Matcher matcher = namePattern.matcher(searchText);
		boolean matchFound = matcher.find();

		if (matchFound) {
			if (gameType == GAME_TYPE.BALL) {
				return Integer.parseInt(matcher.group(2));
			}
			else if (gameType == GAME_TYPE.TBD)
			{
				return Integer.parseInt(matcher.group(1));
			}
		}
		throw new Exception("Couldn't find Rating");		
	}
	
	private String findWinLoss(StringBuilder searchText, GAME_TYPE gameType) throws Exception {
		Pattern namePattern = Pattern.compile("<th>Win - Loss</th>.*?<td>(.*?)</td>.*?<td>(.*?)</td>",Pattern.DOTALL);
		Matcher matcher = namePattern.matcher(searchText);
		boolean matchFound = matcher.find();

		if (matchFound) {
			if (gameType == GAME_TYPE.BALL) {
				return matcher.group(2);
			}
			else if (gameType == GAME_TYPE.TBD)
			{
				return matcher.group(1);
			}
		}
		throw new Exception("Couldn't find Win Loss");		
	}
	
	private ArrayList<Double> findRatings(StringBuilder searchText, GAME_TYPE gameType) {
		Pattern namePattern = Pattern.compile("tbdRatings = \\[(.*?)\\].*?ballRatings = \\[(.*?)\\]",Pattern.DOTALL);
		Matcher matcher = namePattern.matcher(searchText);
		boolean matchFound = matcher.find();

		String listToSplit = "";
		if (matchFound) {
			if (gameType == GAME_TYPE.BALL) {
				listToSplit =  matcher.group(2);
			}
			else if (gameType == GAME_TYPE.TBD)
			{
				listToSplit =  matcher.group(1);
			}
		}
		Pattern splitPattern = Pattern.compile("[,\\s]+", Pattern.DOTALL);		
		String [] ratingText = splitPattern.split(listToSplit);
		ArrayList<Double> ratingNumbers = new ArrayList<Double>();
		for (int i = 0; i < ratingText.length; i++) {
			ratingNumbers.add(Double.parseDouble(ratingText[i].trim()));			
		}
		
		return ratingNumbers;
	}


}
